// Default empty project template
#include "DemoBtn.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/FadeTransition>

using namespace bb::cascades;

#define SW 768
#define SH 1280

DemoBtn::DemoBtn(bb::cascades::Application *app)
: QObject(app)
{
    // create scene document from main.qml asset
    // set parent to created document to ensure it exists for the whole application lifetime
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("_app", this);
    // create root object for the UI
    root = qml->createRootObject<AbstractPane>();
    // set created root object as a scene
    app->setScene(root);

    // Create seed for the random
    // That is needed only once on application startup
    QTime time = QTime::currentTime();
    qsrand((uint)time.msec());

    index = 0;
    containerNow = 0;
    interval = 1000;
    nowTime = 0;
    createTempData();
    createLayout();
    createTimer();
    addTouchHandler();
}

void DemoBtn::createTempData() {
	gamePlayList.append(new ShowText("Show", 1));
	gamePlayList.append(new ShowText("me", 3));
	gamePlayList.append(new ShowText("the", 5));
	gamePlayList.append(new ShowText("meaning", 7));
	gamePlayList.append(new ShowText("of", 9));
	gamePlayList.append(new ShowText("being", 11));
	gamePlayList.append(new ShowText("lonely", 13));
	gamePlayList.append(new ShowText("Show", 15));
	gamePlayList.append(new ShowText("me", 17));
	gamePlayList.append(new ShowText("the", 21));
}

void DemoBtn::addTouchHandler() {
	Container *contentContainer = root->findChild<Container*>("content");
	/*contentContainer->addTouchBehavior(
			TouchBehavior::create()
			.addTouchReaction(TouchType::Down,
					PropagationPhase::Capturing,
					TouchResponse::StartTracking));*/
	connect(contentContainer, SIGNAL(touch(bb::cascades::TouchEvent*)), this, SLOT(showLog("hgfhjgjhgj")));
	/*connect(contentContainer,
	                            SIGNAL(touch(bb::cascades::TouchEvent*)),
	                            this, SLOT(showLog("hgfhjgjhgj")));*/
}

void DemoBtn::createLayout() {
	//Container *contentContainer = root->findChild<Container*>("c6");

	//cList = (Container**)malloc(sizeof(Container*));

	cList.append(root->findChild<Container*>("c0"));
	cList.append(root->findChild<Container*>("c1"));
	cList.append(root->findChild<Container*>("c2"));
	cList.append(root->findChild<Container*>("c3"));
	cList.append(root->findChild<Container*>("c4"));
	cList.append(root->findChild<Container*>("c5"));

	showLog("here");
	//textList = (ShowText*)malloc(sizeof(ShowText));
	showLog("here 1");
	textList.append(new ShowText("Show", 1));
	textList.append(new ShowText("me", 1));
	textList.append(new ShowText("the", 3));
	textList.append(new ShowText("meaning", 5));
	textList.append(new ShowText("of", 7));
	textList.append(new ShowText("being", 9));
	textList.append(new ShowText("lonely", 11));

	/*for (int i = 0 ; i < cList.length(); i++) {
		Container *c = cList[i];
		Label *lbl = c->findChild<Label*>("lbl");
		QString text = textList[i]->text;
			lbl->setText(text);
	}*/
}

void DemoBtn::createTimer() {
	timer = new QTimer(this);
	connect(timer, SIGNAL(timeout()), this, SLOT(update()));
	timer->start(interval);
}

void DemoBtn::showLog(QString msg) {
 fprintf(stdout,"%s\n",msg.toUtf8().constData());
 fflush(stdout);
}

void DemoBtn::changeText(int x, int y) {
	showLog(QString::number(x) + "abcdsaf" + QString::number(y));
}

void DemoBtn::handleTouch(int x, int y) {
	for (int i = 0 ; i < cList.length(); i++) {
		AbsoluteLayoutProperties *layout = cList[i]->findChild<AbsoluteLayoutProperties*>();
		int containerX = layout->positionX();
		int containerY = layout->positionY();
		int containerW = cList[i]->preferredWidth();
		int containerH = cList[i]->preferredHeight();

		//showLog(QString::number(containerX) + "Y:" + QString::number(containerY) + "W:" + QString::number(containerW) + "H:" + QString::number(containerH));
		if (x > containerX && x < containerX + containerW && y > containerY && y < containerY + containerH) {
			showLog(QString::number(containerX) + "abcdsaf" + QString::number(containerY));


			Container *c = cList[i];
			Label *lbl = c->findChild<Label*>("lbl");
			lbl->setText("");

			break;
		}
	}
}

void DemoBtn::update() {
	nowTime += interval;

	if (index == gamePlayList.length()) {
		//timer.stop();
		return;
	}
	if (gamePlayList[index]->time <= nowTime) {
		showLog("show one text");
		Container *c = cList[containerNow];
		Label *lbl = c->findChild<Label*>("lbl");
		QString text = gamePlayList[index]->text;
		lbl->setText(text);

		index ++;
		findOtherContainer();
	}
}

void DemoBtn::findOtherContainer() {
	int now = containerNow;

	do {
		// Get random value between 0-100
		containerNow = randInt(0,cList.length() - 1);
	} while (containerNow == now);
}

int DemoBtn::randInt(int low, int high) {
	// Random number between low and high
	return qrand() % ((high + 1) - low) + low;
}

/*DemoBtn::~DemoBtn() {
	fprintf(stdout, "%s \n", "free");
	fflush(stdout);
	//free(cList);
}*/
